//
// Created by haoy on 2017/7/4.
//

#ifndef MERGE_OPTION_HPP
#define MERGE_OPTION_HPP

#include <iostream>
#include <list>
#include <string>

#include "file.hpp"

class options {
public:
    int parse(int argc, char** argv) {
        bool is_global = true;
        bool is_concat = false;
        for (int i = 1; i < argc; ++i) {
            std::string token(argv[i]);
            if (is_global && token != "-i") {
                global_options.emplace_back(std::move(std::string(token)));
            } else if (token == "-i") {
                if (i + 1 > argc - 1) {
                    std::cerr << "option -i requires a filename followed" << std::endl;
                    return 1;
                }
                std::string fn(argv[i+1]); i += 1;
                file f(input_files.size(), fn);
                input_files.emplace_back(f);
                water_marks.emplace_back("none");
                is_global = false;
            } else if (token == "--water-mark") {
                if (i + 1 > argc - 1) {
                    std::cerr << "option --water-mark requires a string followed" << std::endl;
                    return 1;
                }
                std::string waterMark(argv[i+1]); i += 1;
                water_marks.pop_back();
                water_marks.emplace_back(waterMark);
                is_global = false;
            } else if (token == "-con") {
                if (input_files.size()) {
                    input_files.back().is_concat = true;
                }
            } else if (token == "--help") {
                // print help
            } else if (i == argc - 1) {
                output_filename = std::move(std::string(argv[argc - 1]));
            } else {
                if (token == "-an") {
                    is_audio_disabled = true;
                } else if (token == "-vn") {
                    is_video_disabled = true;
                }
                output_options.emplace_back(std::move(std::string(argv[i])));
            }
        }
        return 0;
    }
    std::list<std::string> global_options;
    std::list<file> input_files;
    std::list<std::string> output_options;
    std::string output_filename;
    bool is_audio_disabled = false;
    bool is_video_disabled = false;

    std::list<std::string> water_marks;
};


#endif //MERGE_OPTION_HPP
